package com.ysd.lis.service.bac.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.bac.*;
import com.ysd.lis.mapper.bac.BacAntibioticsDictMapper;
import com.ysd.lis.request.DeleteParam;
import com.ysd.lis.request.bac.AntibioticsDictList;
import com.ysd.lis.request.bac.AntibioticsDictParam;
import com.ysd.lis.request.bac.BacAntibioticsDictPageParam;
import com.ysd.lis.service.bac.BacAntibioticsDictService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 抗生素 服务实现类
 * </p>
 *
 * @author Bai
 * @since 2024-01-11
 */
@Service
public class BacAntibioticsDictServiceImpl extends ServiceImpl<BacAntibioticsDictMapper, BacAntibioticsDict> implements BacAntibioticsDictService {

    @Autowired
    private BacAntibioticsDictMapper bacAntibioticsDictMapper;

    @Override
    public Result findAntibioticsDictPageList(BacAntibioticsDictPageParam bacAntibioticsDictPage) {
        MPJLambdaWrapper<BacAntibioticsDict> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        Page<BacAntibioticsDict> page = new Page<BacAntibioticsDict>();
        page.setCurrent(bacAntibioticsDictPage.getPageIndex());
        page.setSize(bacAntibioticsDictPage.getPageSize());
        SysUser sysUser = RedisUserManager.getUser();
        lambdaQueryWrapper.selectAll(BacAntibioticsDict.class)
                .selectAs(BacAntClass::getAntClassNa, BacAntibioticsDict::getAntClassNa)
                .selectAs(BacAntSubClass::getSubClassNa, BacAntibioticsDict::getSubClassNa)
                .leftJoin(BacAntClass.class, "sa", p -> p.eq(BacAntClass::getAntClassNo, BacAntibioticsDict::getAntClassNo).eq(BacAntClass::getDelFlag, 0))
                .leftJoin(BacAntSubClass.class, "su", p -> p.eq(BacAntSubClass::getSubClassNo, BacAntibioticsDict::getSubClassNo).eq(BacAntSubClass::getDelFlag, 0));
        if (ToolsUtils.isNotEmpty(sysUser)) {
            lambdaQueryWrapper.eq(BacAntibioticsDict::getOrgId, sysUser.getOrgId());
        }
        if (ToolsUtils.isNotEmpty(bacAntibioticsDictPage.getAntName())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(BacAntibioticsDict::getEnglishName, bacAntibioticsDictPage.getAntName())
                    .or().like(BacAntibioticsDict::getAntName, bacAntibioticsDictPage.getAntName())
                    .or().like(BacAntibioticsDict::getAntCode, bacAntibioticsDictPage.getAntName())
                    .or().like(BacAntibioticsDict::getInputCode,bacAntibioticsDictPage.getAntName()));
        }

        if (!StringUtils.isEmpty(bacAntibioticsDictPage.getSearchValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(BacAntibioticsDict::getAntName, bacAntibioticsDictPage.getSearchValue()).or().like(BacAntibioticsDict::getAntCode, bacAntibioticsDictPage.getSearchValue()).or().like(BacAntibioticsDict::getInputCode, bacAntibioticsDictPage.getSearchValue()));
        }

        if (!BeanUtil.isEmpty(bacAntibioticsDictPage.getAntType())) {
            lambdaQueryWrapper.eq(BacAntibioticsDict::getAntType, bacAntibioticsDictPage.getAntType());
        }

        lambdaQueryWrapper.eq(BacAntibioticsDict::getDelFlag, 0);
        lambdaQueryWrapper.orderByDesc(BacAntibioticsDict::getCreateTime);
        Page<BacAntibioticsDict> labInstrSetupPage = bacAntibioticsDictMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(labInstrSetupPage);
    }

    @Override
    public Result findAntDictList(BacAntibioticsDictPageParam bacAntibioticsDictPage) {
        LambdaQueryWrapper<BacAntibioticsDict> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        SysUser sysUser = RedisUserManager.getUser();
        if (ToolsUtils.isNotEmpty(sysUser)) {
            lambdaQueryWrapper.eq(BacAntibioticsDict::getOrgId, sysUser.getOrgId());
        }
        if (!StringUtils.isEmpty(bacAntibioticsDictPage.getSearchValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(BacAntibioticsDict::getAntName, bacAntibioticsDictPage.getSearchValue()).or().like(BacAntibioticsDict::getAntCode, bacAntibioticsDictPage.getSearchValue()).or().like(BacAntibioticsDict::getInputCode, bacAntibioticsDictPage.getSearchValue()));
        }
        if (!StringUtils.isEmpty(bacAntibioticsDictPage.getFilter())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(BacAntibioticsDict::getAntName, bacAntibioticsDictPage.getFilter()).or().like(BacAntibioticsDict::getAntCode, bacAntibioticsDictPage.getFilter()).or().like(BacAntibioticsDict::getInputCode, bacAntibioticsDictPage.getFilter()));
        }
        if (!BeanUtil.isEmpty(bacAntibioticsDictPage.getAntType())) {
            lambdaQueryWrapper.eq(BacAntibioticsDict::getAntType, bacAntibioticsDictPage.getAntType());
        }
        if(ToolsUtils.isNotEmpty(bacAntibioticsDictPage.getAntClassNo())){
            lambdaQueryWrapper.eq(BacAntibioticsDict::getAntClassNo, bacAntibioticsDictPage.getAntClassNo());
        }
        if(ToolsUtils.isNotEmpty(bacAntibioticsDictPage.getSubClassNo())){
            lambdaQueryWrapper.eq(BacAntibioticsDict::getSubClassNo, bacAntibioticsDictPage.getSubClassNo());
        }
        lambdaQueryWrapper.eq(BacAntibioticsDict::getDelFlag, 0);
        lambdaQueryWrapper.orderByAsc(BacAntibioticsDict::getSeq);
        List<BacAntibioticsDict> labInstrSetupPage = bacAntibioticsDictMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "请求成功", labInstrSetupPage);
    }

    @Override
    public Result findAddAntibioticsDictList() {
        LambdaUpdateWrapper<BacAntibioticsDict> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        SysUser sysUser = RedisUserManager.getUser();
        if (ToolsUtils.isNotEmpty(sysUser)) {
            lambdaUpdateWrapper.eq(BacAntibioticsDict::getOrgId, sysUser.getOrgId());
        }
        lambdaUpdateWrapper.eq(BacAntibioticsDict::getDelFlag, 0);
        lambdaUpdateWrapper.orderByAsc(BacAntibioticsDict::getSeq);
        List<BacAntibioticsDict> labInstrSetups = bacAntibioticsDictMapper.selectList(lambdaUpdateWrapper);
        return Result.succ(1, "查询成功", labInstrSetups);
    }

    @Override
    public Result findAntibioticsDictByInstrId(AntibioticsDictParam dictParam) {
        MPJLambdaWrapper<BacAntibioticsDict> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(BacAntibioticsDict.class).eq(BacAntibioticsDict::getDelFlag, 0);
        if (StrUtil.isNotEmpty(dictParam.getKeyWords())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(BacAntibioticsDict::getAntCode, dictParam.getKeyWords()).or().like(BacAntibioticsDict::getAntName, dictParam.getKeyWords()));
        }
        if (StrUtil.isNotEmpty(dictParam.getInstrCode())) {
            StringBuffer existsSql = new StringBuffer("SELECT 1 FROM BAC_INSTR_ANT_DICT t2 WHERE t.ANT_NO = t2.ANT_CODE AND t2.INSTR_CODE = '" + dictParam.getInstrCode() + "' ");
            lambdaQueryWrapper.notExists(existsSql.toString());
        }
        List<BacAntibioticsDict> bacAntibioticsDicts = bacAntibioticsDictMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功！", bacAntibioticsDicts);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addAntibioticsDict(BacAntibioticsDict bacAntibioticsDict) {
        SysUser sysUser = RedisUserManager.getUser();
        if (ToolsUtils.isNotEmpty(sysUser)) {
            bacAntibioticsDict.setOrgId(sysUser.getOrgId());
        }
        boolean save = this.save(bacAntibioticsDict);
        if (save) {
            return Result.succ(1, "添加成功！", null);
        } else {
            return Result.fail("添加失败！");
        }
    }

    @Override
    public Result editAntibioticsDict(BacAntibioticsDict bacAntibioticsDict) {
        boolean b = this.updateById(bacAntibioticsDict);
        if (b) {
            return Result.succ(1, "编辑成功！", null);
        } else {
            return Result.fail("编辑失败！");
        }
    }

    @Override
    public Result deleteAntibioticsDict(DeleteParam deleteParam) {
        if (ToolsUtils.isEmpty(deleteParam)) {
            return Result.fail("入参不可为空！");
        }
        if (ToolsUtils.isEmpty(deleteParam.getIds())) {
            return Result.fail("入参不可为空！");
        }
        String ids = deleteParam.getIds();
        String[] array = ids.split(",");
        List<String> idList = Arrays.asList(array);
        boolean b = this.removeByIds(idList);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result updateBachAntibioticsDict(AntibioticsDictList antibioticsDictList) {
        List<BacAntibioticsDict> bacAntibioticsDictList = antibioticsDictList.getBacAntibioticsDictList();

        boolean b = this.saveOrUpdateBatch(bacAntibioticsDictList);
        if (b) {
            return Result.succ(1, "批量保存成功", null);
        } else {
            return Result.fail(400, "批量保存失败", "");
        }
    }

    @Override
    public Result findPageAntibioticsDictByInstrId(AntibioticsDictParam dictParam) {
        Page<BacAntibioticsDict> page = new Page<>();
        page.setCurrent(dictParam.getPageIndex());
        page.setSize(dictParam.getPageSize());
        MPJLambdaWrapper<BacAntibioticsDict> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(BacAntibioticsDict.class).eq(BacAntibioticsDict::getDelFlag, 0);
        if (StrUtil.isNotEmpty(dictParam.getKeyWords())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(BacAntibioticsDict::getAntCode, dictParam.getKeyWords()).or().like(BacAntibioticsDict::getAntName, dictParam.getKeyWords()));
        }
        if (StrUtil.isNotEmpty(dictParam.getInstrCode())) {
            StringBuffer existsSql = new StringBuffer("SELECT 1 FROM BAC_INSTR_ANT_DICT t2 WHERE t.ANT_NO = t2.ANT_CODE AND t2.INSTR_CODE = '" + dictParam.getInstrCode() + "' ");
            lambdaQueryWrapper.notExists(existsSql.toString());
        }
        Page<BacAntibioticsDict> bacAntibioticsDicts = bacAntibioticsDictMapper.selectPage(page,lambdaQueryWrapper);
        return Result.succ(1, "查询成功！", bacAntibioticsDicts);
    }


    @Override
    public Result findInstrAntibDictsByInstrId(AntibioticsDictParam dictParam) {
        Assert.hasText(dictParam.getInstrCode(), "id must not be empty");
        MPJLambdaWrapper<BacAntibioticsDict> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper.selectAll(BacAntibioticsDict.class);
        mpjLambdaWrapper.selectAs(BacInstrAntibDict::getId, BacAntibioticsDict::getInstrAntibDictId);
        mpjLambdaWrapper.selectAs(BacInstrAntibDict::getInputAntCode, BacAntibioticsDict::getInputAntCode);
        mpjLambdaWrapper.selectAs(BacInstrAntibDict::getInputAntNa, BacAntibioticsDict::getInputAntNa);
        mpjLambdaWrapper.leftJoin(BacInstrAntibDict.class, BacInstrAntibDict::getAntCode, BacAntibioticsDict::getAntCode);
        mpjLambdaWrapper.eq(BacInstrAntibDict::getInstrCode, dictParam.getInstrCode());
        mpjLambdaWrapper.eq(BacAntibioticsDict::getDelFlag, 0).orderByAsc(BacAntibioticsDict::getSeq);
        if (StrUtil.isNotEmpty(dictParam.getKeyWords())) {
            mpjLambdaWrapper.and(wrapper -> wrapper.like(BacAntibioticsDict::getAntCode, dictParam.getKeyWords()).or().like(BacAntibioticsDict::getAntName, dictParam.getKeyWords()));
        }
        List<BacAntibioticsDict> bacAntibioticsDicts = bacAntibioticsDictMapper.selectList(mpjLambdaWrapper);
        return Result.succ(1, "查询成功", bacAntibioticsDicts);
    }
}
